package com.taglab.chic.search;

import java.util.Iterator;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

/**
 * 
 * @author Markus Kobler
 */
public class SearchResults implements Iterable<SearchResult> {

  private final Query query;

  private final int totalResults;

  private final List<SearchResult> results;

  public SearchResults(Query query, Collection<SearchResult> results, int totalResults) {
    this.query = query;
    this.results = new ArrayList<SearchResult>(results);
    this.totalResults = totalResults;
  }

  public Query getQuery() {
    return query;
  }

  public String getSearchTerms() {
    return query.getSearchTerms();
  }

  public int getStartPage() {
    return query.getStartPage();
  }

  public int getItemsPerPage() {
    return query.getCount();
  }

  
  public List<SearchResult> getResults() {
    return Collections.unmodifiableList(results);
  }

  public int getTotalResults() {
    return totalResults;
  }

  public Iterator<SearchResult> iterator() {
    return getResults().iterator();
  }


  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("SearchResults");
    sb.append("{query=").append(query);
    sb.append(", totalResults=").append(totalResults);
    sb.append(", results:\n");
    int count = 1;
    for( SearchResult result : results ) {
      sb.append(count++).append(":").append(result.toLongString()).append("\n");
    }
    sb.append('}');
    return sb.toString();
  }
}
